<template>
	<div class="addProject business-workAdd">
		<div class="addForm zll-form">
			<el-form :model="addForm" :rules="rules" ref="addForm" class="demo-ruleForm">
				<el-row :gutter="40">
					<el-col :span="8">
						<el-form-item class="formList" prop="date" label="下发日期：">
							<el-input clearable class="input_right" v-model="sendTime"
									  disabled></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item class="formList" prop="name" label="下发人：">
							<el-input clearable class="input_right" v-model="sendUserName"
									  disabled></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item class="formList" prop="type" label="需求类型：">
							<el-input clearable class="input_right" v-model="addForm.orderType"
									  disabled></el-input>
						</el-form-item>
					</el-col>
				</el-row>
				<div class="tableList">
					<table border="1">
						<tr>
							<th width="120">订单号</th>
							<th width="200">客户</th>
							<th>品类</th>
							<th width="200">产品名称</th>
							<th width="250">规格</th>
							<th>单件万支</th>
							<th>件数</th>
							<th>合计(万支)</th>
							<th>交货期(天)</th>
							<th>备注</th>
						</tr>
						<tr class="add_Table" v-for="(item,index) in orderTableList" :key="index">
							<td><p>{{ item.orderNum }}
								<span @click="copyValue(item.orderNum)"  style="color: #1890ff;cursor: pointer;position: absolute;margin-left: 5px;top: 2px;"><i style="font-size: 14px;" class="el-icon-document-copy"></i></span>
							</p></td>
							<td>
								<p>{{ item.customer }}
								<span @click="copyValue(item.customer)"  style="color: #1890ff;cursor: pointer;position: absolute;margin-left: 5px;top: 2px;"><i style="font-size: 14px;" class="el-icon-document-copy"></i></span>
							</p></td>
							<td><p>{{item.itemTypeId}}</p></td>
							<td><p>{{ item.productName }}
								<span @click="copyValue(item.productName)"  style="color: #1890ff;cursor: pointer;position: absolute;margin-left: 5px;top: 2px;"><i style="font-size: 14px;" class="el-icon-document-copy"></i></span>
							</p></td>
							<td><p>{{ item.gradeValue }}
								<span @click="copyValue(item.gradeValue)"  style="color: #1890ff;cursor: pointer;position: absolute;margin-left: 5px;top: 2px;"><i style="font-size: 14px;" class="el-icon-document-copy"></i></span>
							</p></td>
							<td><p>{{ item.thNumber }}</p></td>
							<td><p>{{ item.peieceNum }}</p></td>
							<td><p>{{ item.totalNum }}</p></td>
							<td><p>{{ item.goodsDay }}</p></td>
							<td><p>{{ item.remark }}</p></td>
						</tr>
					</table>
				</div>
				<div v-if="type == 'see'">

					<!----生产需求---->
					<div class="nav_list">
						<div class="_title"><i class="iconfont icon-dici"></i>生产需求</div>
						<el-row :gutter="40">
							<el-col :span="8">
								<el-form-item class="formList" prop="data12" label="计划类型：">
									<el-input clearable class="input_right" v-model="planManageDetail.planTypeCn"
											  disabled></el-input>
								</el-form-item>
								<el-form-item class="formList" prop="data13" label="计划号：">
									<el-input clearable class="input_right" v-model="planManageDetail.planCode"
											  disabled></el-input>
								</el-form-item>
							</el-col>
							<el-col :span="8">
								<el-form-item class="formList" prop="data14" label="计划状态：">
									<el-input clearable class="input_right" v-model="planManageDetail.planStatusCn"
											  disabled></el-input>
								</el-form-item>
								<el-form-item class="formList" prop="data15" label="计划日期：">
									<el-input clearable class="input_right" v-model="planManageDetail.createTimeFt"
											  disabled></el-input>
								</el-form-item>
							</el-col>
							<el-col :span="8">
								<el-form-item class="formList"></el-form-item>
								<el-form-item class="formList" prop="data17" label="完成日期：">
									<el-input clearable class="input_right" v-model="planManageDetail.completeTime"
											  disabled></el-input>
								</el-form-item>
							</el-col>
						</el-row>
					</div>
					<!----发货记录---->
					<div class="nav_list">
						<div class="_title"><i class="iconfont icon-daifahuo"></i>发货记录</div>
						<div class="tableList" style="margin-top: 0;">
							<table border="1">
								<tr>
									<th>发货单号</th>
									<th>出库类型</th>
									<th>出库号</th>
									<th>实发件数</th>
									<th>实发合计（万支）</th>
									<th>出库日期</th>
									<th>出库人</th>
								</tr>
								<tr v-if="goodsTableList.length == 0" class="noData">
									<td colspan="8">暂无数据</td>
								</tr>
								<tr v-else class="add_Table" v-for="(item, index) in goodsTableList" :key="index">
									<td><p>{{ item.orderOutCode }}</p></td>
									<td><p>{{ item.storageOutTypeCn }}</p></td>
									<td><p>{{ item.storageOutCode }}</p></td>
									<td><p>{{ item.storageOutCount }}</p></td>
									<td><p>{{ item.thNumbers }}</p></td>
									<td><p>{{ item.storageOutTime }}</p></td>
									<td><p>{{ item.storageOutUser }}</p></td>
								</tr>
								<tr v-if="goodsTableList.length > 0">
									<td colspan="3">合计</td>
									<td><p>{{goodsTotal.storageOutCount}}</p></td>
									<td><p>{{goodsTotal.thNumber}}</p></td>
									<td></td>
									<td></td>
								</tr>
							</table>
						</div>
					</div>
					<template v-if="addForm.goodsStatus == '1'">
						<!----退货---->
						<div class="nav_list">
							<div class="_title"><i class="iconfont icon-daifahuo"></i>退货</div>
							<div class="tableList" style="margin-top: 0;">
								<table border="1" style="table-layout: fixed;">
									<tr>
										<th>退货号</th>
										<th>应退件数</th>
										<th>退款件数</th>
										<th>物流到货</th>
										<th>物流赔损</th>
										<th>入库</th>
										<th>入库客损</th>
										<th>状态</th>
										<th>日期</th>
									</tr>
									<tr class="add_Table">
										<td><p>{{ backGoods.orderBackCode }}</p></td>
										<td><p>{{ backGoods.backNeedCount }}</p></td>
										<td><p>{{ backGoods.peieceNum }}</p></td>
										<td><p>{{ backGoods.wuliuCount }}</p></td>
										<td><p>{{ backGoods.wuliuLose }}</p></td>
										<td><p>{{ backGoods.backOunt }}</p></td>
										<td><p>{{ backGoods.backCountLose }}</p></td>
										<td><p>{{ backGoods.orderBackStatusCn }}</p></td>
										<td><p>{{ backGoods.createTimeFt }}</p></td>
									</tr>
								</table>
							</div>
						</div>
						<!--<div class="nav_list">-->
							<!--<div class="_title"><i class="iconfont icon-tongji-caigou-caigoutuihuozhuangtaifenxi"></i>自动核算-->
							<!--</div>-->
							<!--<div class="tableList" style="margin-top: 0;">-->
								<!--<table border="1" style="table-layout: fixed;">-->
									<!--<tr>-->
										<!--<th>订单件数</th>-->
										<!--<th>实发支数</th>-->
										<!--<th>应退件数</th>-->
										<!--<th>退款件数</th>-->
										<!--<th>结算件数</th>-->
									<!--</tr>-->
									<!--<tr class="add_Table">-->
										<!--<td><p>{{ addForm.data1 }}</p></td>-->
										<!--<td><p>{{ addForm.data2 }}</p></td>-->
										<!--<td><p>{{ addForm.data3 }}</p></td>-->
										<!--<td><p>{{ addForm.data4 }}</p></td>-->
										<!--<td><p>{{ addForm.data5 }}</p></td>-->
									<!--</tr>-->
								<!--</table>-->
							<!--</div>-->
						<!--</div>-->

					</template>
				</div>
			</el-form>
		</div>
	</div>
</template>
<script>
	import {getStore} from "@/plugins/localStore";
	import {formatDate} from "@/plugins/formatDate";
	export default {
		props: ['editData', 'type', 'selectItems'],
		data() {
			return {
				addForm: {
					orderType:'',
					goodsStatus:'',
				},
				rules: {

				},

				sendTime:'',
				sendUserName:'',

				items: [],
				count: 0,
				user: {},

				orderTableList:[],
				goodsTableList:[],
				goodsTotal:{storageOutCount:'',thNumber:''},
				planManageDetail:{},
				backGoods:{},


			}
		},
		methods: {

			copyValue(textContent) {
				let inputDom = document.createElement('textarea'); // js创建一个文本框
				document.body.appendChild(inputDom); //将文本框暂时创建到实例里面
				inputDom.value = textContent;//将需要复制的内容赋值到创建的文本框中
				inputDom.select();//选中文本框中的内容
				inputDom.focus();
				document.execCommand('copy'); //执行复制操作
				document.body.removeChild(inputDom); //最后移出
				this.$message.success('复制成功')
			},
			getItemTypeName(order){
				let self = this;
				let data = self.items.find((item)=> item.id == order.itemTypeId);
				return data?data.itemName:'';
			},

			submitForm(val) {
				let self = this;
				self.$refs['addForm'].validate((valid) => {
					if (valid) {
						self.operate();
					} else {
						return false;
					}
				});
			},

			getPlanByOrderId(id) {
				let self = this;
				self.planManageDetail ={};
				self.$http({
					url: "/jinl/plan/getPlanByOrderId",
					method: "post",
					params:{orderId:id}
				}).then(resp => {
					if (resp.success) {
						self.planManageDetail = resp.result||{};
					}
				});
			},

			getOrderBackInfoByOrderId(id) {
				let self = this;
				self.backGoods ={};
				self.$http({
					url: "/jinl/orderBack/getOrderBackInfoByOrderId",
					method: "post",
					params:{orderId:id}
				}).then(resp => {
					if (resp.success) {
						self.backGoods = resp.result||{};
						let wuliuCount = self.backGoods.wuliuCount;
						let peieceNum = self.backGoods.peieceNum;
						let wuliuLose = '';
						if(wuliuCount&&peieceNum){
							wuliuLose = parseFloat(peieceNum) -  parseFloat(wuliuCount);
						}
						self.backGoods.wuliuLose = wuliuLose.toFixed(4);
						let backNeedCount = self.backGoods.backNeedCount;
						let backOunt = self.backGoods.backOunt;
						let backCountLose = '';
						if(backNeedCount&&backOunt){
							backCountLose = parseFloat(backNeedCount) -  parseFloat(backOunt);
						}
						self.backGoods.backCountLose = backCountLose.toFixed(4);
					}
				});
			},



			getProductStorageByOrderId(id) {
				let self = this;
				self.goodsTableList = [];
				self.goodsTotal = {storageOutCount:'',thNumber:''};
				self.$http({
					url: "/jinl/productStorageOut/getProductStorageByOrderId",
					method: "post",
					params:{orderId:id}
				}).then(resp => {
					if (resp.success) {
						let goodsTableList = resp.result||[];
						let storageOutCounts = 0;
						let thNumbers = 0.000;
						if(goodsTableList.length == 0){
							return;
						}
						console.log('====goodsTableList====')
						console.log(goodsTableList)
						for(let data of goodsTableList){
							if(data.storageOutStatus == '0'){
								continue;
							}
							let storageOutCount = data.storageOutCount;
							let thNumber = data.thNumber;
							storageOutCounts+= parseInt(storageOutCount);
							thNumbers+= parseFloat(thNumber);
							data.thNumbers = (thNumber*storageOutCount).toFixed(4);
							self.goodsTableList.push(data);
						}
						thNumbers = (thNumbers*storageOutCounts).toFixed(4);
						self.goodsTotal.storageOutCount = storageOutCounts;
						self.goodsTotal.thNumber = thNumbers;
					}
				});
			},

			operate() {
				let self = this;
				let params;
				let url =  '';
				if(this.type == 'devolve'){
					params = JSON.parse(JSON.stringify(this.orderTableList));
					for(let data of params){
						data.sendNeedsUser = self.sendUserName;
						data.sendNeedsTime = self.sendTime;
					}
					url = '/jinl/order/updateSendNeedOrder';
				}
				if (self.count == 0) {
					self.$http({
						url: url,
						method: "post",
						data: JSON.stringify(params),
						dataType: 'json',
						contentType: "application/json",
					}).then(resp => {
						if (resp.success) {
							self.$notify({title: '提示', message: "操作成功！", type: 'success'});
							self.count--;
							self.$eventBus.$emit("updateOrder2List");
							self.$eventBus.$emit("getOrderNum");
							return;
						}
						self.$notify({title: '提示', message: resp.message, type: 'error'});
						self.count--;
					});
					self.count++;
				}
			},

		},

		watch: {
			selectItems(val) {
				this.items = val;
				console.log('---this.items----')
				console.log(this.items)
			},
			editData(val) {
				console.log(val)
				this.user = getStore();
				this.count = 0;
				if(this.type == 'devolve'){
					this.addForm.orderType =  val&&val.length >0?val[0].orderType:'';
					this.sendTime = formatDate(new Date(), 'yyyy-MM-dd hh:mm:ss');
					this.sendUserName = this.user.userName;
					this.orderTableList = val;
				}else{
					this.addForm.orderType = val.orderType;
					this.sendTime = val.sendNeedsTime;
					this.sendUserName = val.sendNeedsUser;
					this.orderTableList.push(val);
				}
				this.addForm.goodsStatus = val.goodsStatus;
				this.getProductStorageByOrderId(val.id);
				this.getPlanByOrderId(val.id);
				if(this.addForm.goodsStatus == '1'){
					this.getOrderBackInfoByOrderId(val.id);
				}
			},

		},

		mounted() {

		},
		components: {}
	}
</script>
<style lang="scss" scoped>
	@import "@/style/dialog.scss";

	.business-workAdd.addProject {
		.nav_list {
			margin-top: 20px;

			._title {
				line-height: 40px;
				color: #00D1D0;

				i {
					font-size: 16px;
					margin-right: 5px;
				}
			}
		}

		.formList {
			margin-bottom: 0px;
		}

		.tableList {
			table {
				table-layout: auto;

				tr {
					background: #fff !important;

					td {
						padding: 1px 3px;position: relative;
						p {
							position: relative;
						}
					}

					&.noData {
						color: #999;
						height: 40px;
					}

					._span {
						width: 16.666%;
						float: right;
						color: #1890ff;
						cursor: pointer;

						&:hover {
							opacity: .7;
						}
					}
				}

				.edit {
					color: #1890ff;
					padding: 0 5px;
					cursor: pointer;

					&.icons {
						font-size: 16px;
					}

					&:hover {
						opacity: .7;
					}
				}
			}
		}
	}
</style>